package edu.zut.arrays;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ListSort {

    public static void main(String[] args) {
        testSortStu();
    }

    static void testSort(){
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("z");
        list.add("k");
        list.add("m");
        System.out.println(list);
        //升序输出    Comparator  比较器   自定义比较规则
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
//        直接升序
        Collections.sort(list);
        System.out.println("定义比较规则后的结果：" + list);
    }

    static void testSortStu(){
        List<Stu> list = new ArrayList<Stu>();
        list.add(new Stu("jack",90));
        list.add(new Stu("tom",80));
        list.add(new Stu("rose",85));
        list.add(new Stu("lucy",77));
        //想按照成绩升序
        /*list.sort(new Comparator<Stu>() {
            @Override
            public int compare(Stu o1, Stu o2) {
                return o1.score - o2.score;
            }
        });*/
        //利用collections进行排序，
        // 如果集合中的元素是对象，此对象必须实现 Comparable接口,重写compareTo
        Collections.sort(list);
        System.out.println(list);
    }

}

class Stu implements Comparable<Stu>{

    String name;
    int score;

    Stu(String name,int score){
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Stu o) {
        return this.score - o.score;
    }
}

